package com.mayisoftware.mayioa.business.plan.impl.controller;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanAchievement;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanArchService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/plan/achievement")
public class PlanAchController extends BaseController{
    private String prefix = "plan/achievement";

    @Autowired
    IPlanArchService planArchService;

    @RequiresPermissions("plan:achievement:view")
    @GetMapping()
    public String planAchievement()
    {
        return prefix + "/achievement";
    }

    /**
     * 显示绩效列表
     * @param planAchievement
     * @return
     */
    @RequiresPermissions("plan:achievement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlanAchievement planAchievement)
    {
        startPage();
        List<PlanAchievement> list = planArchService.selectPlanAchList(planAchievement);
        return getDataTable(list);
    }

    /**
     * 新增计划绩效页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增计划绩效
     * @param planAchievement
     * @return
     */
    @RequiresPermissions("plan:achievement:add")
    @Log(title = "计划绩效管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(PlanAchievement planAchievement){
        return toAjax(planArchService.insertPlanAch(planAchievement));
    }

    /**
     * 修改计划绩效
     * @param planAchId 绩效id
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{planAchId}")
    public String edit(@PathVariable("planAchId") Long planAchId, ModelMap mmap){
        mmap.put("planAch", planArchService.selectPlanAchById(planAchId));
        return prefix + "/edit";
    }

    @RequiresPermissions("plan:achievement:edit")
    @Log(title = "计划绩效管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(PlanAchievement planAchievement){
        planAchievement.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(planArchService.updatePlanAch(planAchievement));
    }

    @RequiresPermissions("plan:achievement:remove")
    @Log(title = "计划绩效管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            return toAjax(planArchService.deletePlanAchByIds(ids));
        }catch (Exception e){
            return toAjax(0);
        }
    }
}
